package com.code.coinprice.services.impl;

import com.code.coinprice.configs.NettyConfig;
import com.code.coinprice.services.PushService;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.springframework.stereotype.Service;

import java.util.concurrent.ConcurrentHashMap;

@Service
public class PushServiceImpl implements PushService {

    /**
     * 模拟向客户端实时发送币种价格
     *
     * @param clientChannelId 客户端通道唯一标识
     * @param msg             发送的消息
     */
    @Override
    public void pushMsgToOne(String clientChannelId, String msg) {
        ConcurrentHashMap<String, Channel> platformChannelMap = NettyConfig.getPlatformChannelMap();
        Channel channel = platformChannelMap.get(clientChannelId);
        channel.writeAndFlush(new TextWebSocketFrame(msg));
    }

    @Override
    public void pushMsgToAll(String msg) {
        NettyConfig.getChannelGroup().writeAndFlush(new TextWebSocketFrame(msg));
    }
}
